#include "ECS/Component/PLookAtCameraComponent.h"
#include "ECS/Component/PTransformComponent.h"

#include "glm/gtx/matrix_decompose.hpp"

namespace Piccolo
{
    const glm::mat4& PLookAtCameraComponent::GetProjMatrix()
    {
        mProjMatrix = glm::perspective(glm::radians(mFov), mAspect, mNearPlane, mFarPlane);
        return mProjMatrix;
    }

    const glm::mat4& PLookAtCameraComponent::GetViewMatrix()
    {
        PEntity* owner = GetOwner();
        if (PEntity::HasComponent<PTransformComponent>(owner))
        {
            auto& transformComponent = owner->GetComponent<PTransformComponent>();

            float yaw = transformComponent.rotation.x;
            float pitch = transformComponent.rotation.y;

            glm::vec3 direction;
            direction.x = cos(glm::radians(pitch)) * sin(glm::radians(yaw));
            direction.y = sin(glm::radians(pitch));
            direction.z = cos(glm::radians(pitch)) * cos(glm::radians(yaw));

            transformComponent.position = mTarget + direction * mRadius;

            mViewMatrix = glm::lookAt(transformComponent.position, mTarget, mWorldUp);
        }
        return mViewMatrix;
    }

    void PLookAtCameraComponent::SetViewMat(const glm::mat4 &viewMat)
    {
        // TODO
    }
}
